// src/store/index.ts
import { createPinia, defineStore } from 'pinia'

// 创建Pinia实例
const pinia = createPinia()

// 类型声明
interface StoreModule {
    state?: () => Record<string, any>
    getters?: Record<string, (...args: any[]) => any>
    actions?: Record<string, (...args: any[]) => any>
}

// 动态加载模块
const modules = import.meta.glob<{ default: StoreModule }>('./modules/*.store.ts', {
    eager: true
})

// 自动注册模块
const registeredStores: Record<string, ReturnType<typeof defineStore>> = {}
Object.entries(modules).forEach(([path, module]) => {
    const moduleName = path
        .split('/')
        .pop()
        ?.replace(/\.store\.ts$/, '') || ''

    const storeDefinition = module.default
    const useStore = defineStore(moduleName, storeDefinition)

    registeredStores[moduleName] = useStore
})

// 统一访问接口
export const useDynamicStore = (name: string) => {
    const store = registeredStores[name]
    return store?.()
}

export default pinia